package com.tianqi.ui.gui;

import com.tianqi.ui.service.Service;
import com.tianqi.ui.service.Service2;
import com.tianqi.ui.service.Service3;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class MainGui2 {

    private Service service = new Service();
    private Service2 service2 = new Service2();
    private Service3 service3 = new Service3();

    private JPanel contentPanel = new JPanel();
    private DefaultListModel<String> result;
    private DefaultListModel<String> result2;
    private DefaultListModel<String> result3;

    private JLabel lableCity;
    private JTextField searchCity;
    private JButton buttonQuery;
    private JButton buttonTips;
    private JButton buttonYesterday;
    private JScrollPane scrollPane;
    private JList listInfo;
    private JList listInfo2;
    private JList listInfo3;

    public MainGui2() {
        JFrame jf = new JFrame("天气小灵通");
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.setBounds(200,200,1000,800);
        jf.setContentPane(contentPanel);
        jf.setLayout(null);

        //检索框
        lableCity = new JLabel("城市：");
        lableCity.setBounds(30,30,100,30);
        Font font = new Font(null,Font.BOLD,28);
        lableCity.setFont(font);
        contentPanel.add(lableCity);

        // 文本框
        searchCity = new JTextField();
        searchCity.setBounds(120,30,200,30);
        searchCity.setFont(font);
        contentPanel.add(searchCity);

        buttonQuery = new JButton("查询");
        buttonQuery.setBounds(350,30,100,30);
        buttonQuery.setFont(font);
        buttonQuery.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                result.removeAllElements();
                String city =searchCity.getText();
                result = service.query(city);
                System.out.println(city);
                listInfo.setModel(result);

            }
        });
        contentPanel.add(buttonQuery);

        // tips开始
        buttonTips = new JButton("温馨提示");
        buttonTips.setBounds(450,30,180,30);
        buttonTips.setFont(font);
        buttonTips.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                result.removeAllElements();
                String city =searchCity.getText();
                result = service2.query(city);
                System.out.println(city);
                listInfo.setModel(result);

            }
        });
        contentPanel.add(buttonTips);
        //

        //昨天温度
        buttonYesterday = new JButton("往日温度");
        buttonYesterday.setBounds(630,30,180,30);
        buttonYesterday.setFont(font);
        buttonYesterday.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                result.removeAllElements();
                String city =searchCity.getText();
                result = service3.query(city);
                System.out.println(city);
                listInfo3.setModel(result);

            }
        });
        contentPanel.add(buttonYesterday);
        //



        listInfo = new JList<>();
        listInfo.setBounds(0,0,990,600);
        listInfo.setFont(font);
        scrollPane = new JScrollPane(listInfo);
        scrollPane.setBounds(5,90,960,600);
        contentPanel.add(scrollPane);

        // list2
        listInfo2 = new JList<>();
        listInfo2.setBounds(0,0,990,600);
        listInfo2.setFont(font);
        scrollPane = new JScrollPane(listInfo2);
        scrollPane.setBounds(5,90,960,600);
        contentPanel.add(scrollPane);
        //

        // list3
        listInfo3 = new JList<>();
        listInfo3.setBounds(0,0,990,600);
        listInfo3.setFont(font);
        scrollPane = new JScrollPane(listInfo3);
        scrollPane.setBounds(5,90,960,600);
        contentPanel.add(scrollPane);
        //

        result = service.query(null);
        result2 = service2.query(null);
        result3 = service3.query(null);

        listInfo.setModel(result2);
        listInfo.setModel(result3);
        listInfo.setModel(result);


        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new MainGui2();

    }

}
